var sql = require('../api/connect');
var list=function(req,res){
	var seachsql='';
	var page=5*req.query.page-5;
	if(req.query.step!=''){
		seachsql+='and step="'+req.query.step+'" ';
	}
	if(req.query.pass!=''){
		seachsql+='and pass="'+req.query.pass+'" ';
	}
	if(req.query.typeId!=''){
		seachsql+='and type_id="'+req.query.typeId+'" ';		
	}
	var sqltext='select count(*) from project where 1=1 ' + seachsql + ';select * from project where 1=1 ' + seachsql + ' limit ' + page + ',5';
	sql.getConnection(function(err, connection) {
        if(err) throw err;
        connection.query(sqltext,function (err, results) {
            connection.release();
            if (err){
                throw err
            }else{
            	console.log()
                // 计算总页数
                var allCount = results[0][0]['count(*)'];
                var allPage = parseInt(allCount)/5;
                var pageStr = allPage.toString();
                // 不能被整除
                if (pageStr.indexOf('.')>0) {
                    allPage = parseInt(pageStr.split('.')[0]) + 1; 
                }
                var userList = results[1];
                res.end(JSON.stringify({msg:'操作成功',status:'200',totalPages:allCount,data:userList}));
           }
        })
    })
}
module.exports=list;
